package com.ling.service;

import com.ling.bean.QuestionUserList;
import com.ling.bean.QuestionUserListExample;
import com.ling.dao.QuestionUserListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionUserListService {

    private final QuestionUserListMapper questionUserListMapper;

    @Autowired
    public QuestionUserListService(QuestionUserListMapper questionUserListMapper) {
        this.questionUserListMapper = questionUserListMapper;
    }

    //获取待回答的问题，hasread=0
    public List<QuestionUserList> getToDoQuestion(String userId) {
        return questionUserListMapper.getToDoQuestion(userId);
    }

    //修改hasread为1
    public void updateHasread(String questionUserListId) {
        QuestionUserList questionUserList = new QuestionUserList();
        questionUserList.setId(questionUserListId);
        questionUserList.setHasread(true);
        questionUserListMapper.updateByPrimaryKeySelective(questionUserList);
    }

    //添加邀请记录
    public void addQuestionUserList(QuestionUserList questionUserList) {
        if (!isExistQuestionUserList(questionUserList)) {
            questionUserListMapper.insertSelective(questionUserList);
        }
    }

    //判断是否存在该记录
    private boolean isExistQuestionUserList(QuestionUserList questionUserList) {
        QuestionUserListExample questionUserListExample = new QuestionUserListExample();
        questionUserListExample.createCriteria().andUseridEqualTo(questionUserList.getUserid()).andQuestionidEqualTo(questionUserList.getQuestionid());
        return questionUserListMapper.countByExample(questionUserListExample)>0;
    }
}
